local filename = "agentcmd.lua"

local skynet = require "skynet"

local filelog = require "filelog"

-- 每个函数都需要用的这个 service，要知道调用者是谁，这样可以直接使用调用者的其他函数
local service = nil
-- 放到table里面，只是为了方便外界更改这个service
local AgentCMD = {
	service = nil
}

function AgentCMD.process(session, source, event, ...)
	-- 这段只是为了方便写代码一下，不想每个函数调用前都要加上
	-- local service = AgentCMD.service
	if not service then
		service = AgentCMD.service
	end
	local f = AgentCMD[event] 
	if f == nil then
		filelog.sys_error(filename.." line22 Agent AgentCMD.process invalid event:"..event)
		return nil
	end
	return f(...)
end

function AgentCMD.create_session(conf)
	return service:create_session(conf)
end

-- 这里的代码还有点疑惑。 
function AgentCMD.close(is_client, ...)
    if is_client == 1 then
        service:agentexit()
        return true
    end

	--延迟释放agent保证agent能正常处理完
	if service.state ~= EGateAgentState.GATE_AGENTSTATE_LOGINED then
		service:agentexit(true)
		skynet.sleep(500)
		service:exit_service()
	end
	return true
end

function AgentCMD.gamenode_init(nodename)
	service:gamenode_init(nodename)
end

function AgentCMD.disconnect(fd, isheart)
	return service:disconnect(fd, isheart)
end

function AgentCMD.reconnect(conf)
	return service:reconnect(conf)
end

function AgentCMD.notify_client(msgname, msgbody, retcode, retstr)
	return service:notify_client(msgname, msgbody, retcode, retstr)
end

function AgentCMD.myroleinfo()
	return service:myroleinfo()
end

-- 对money的几个接口，纯粹转发到 service去处理
function AgentCMD.change_money(...)
	return service:change_money(...)
end

function AgentCMD.charge_diamond(...)
	return service:charge_diamond(...)
end

function AgentCMD.charge_fangka(...)
	return service:charge_fangka(...)
end

function AgentCMD.charge_prop(...)
	return service:charge_prop(...)
end

function AgentCMD.game_sub_fuwufei(...)
	return service:game_sub_fuwufei(...)
end

function AgentCMD.game_balance_diamond(...)
	return service:game_balance_diamond(...)
end

function AgentCMD.game_half_diamond(...)
	return service:game_half_diamond(...)
end

function AgentCMD.game_sub_fangka(...)
	return service:game_sub_fangka(...)
end

function AgentCMD.game_balance_fangka(...)
	return service:game_balance_fangka(...)
end

function AgentCMD.updateroleinfo(...)
	return service:updateroleinfo(...)
end

function AgentCMD.kickoff()
    service:notify_client("kickroleN", {rid = service.rid})
	return service:disconnect(service.client_fd, true)
end

function AgentCMD.tablechatnotify(chatmsg)
	return service:notify_client("tablechatN", chatmsg)
end

function AgentCMD.tableinvitenotify(invitemsg)
	return service:notify_client("tableinviteN", invitemsg)
end

function AgentCMD.enter_table(...)
	return service:enter_table(...)
end

function AgentCMD.leave_table(...)
	return service:leave_table(...)
end

function AgentCMD.set_gamenode_config_ab(game, ab)
	service.gamenode_config_ab[game] = ab
end

function AgentCMD.check_role_state(enter_diamond, is_cointable)
    if service.gamenode ~= "" or service.table_index > 0 then 
        return true
    end
    if service.rid == 0 or not service.money then
        return true
    end

    local diamond = service.money.diamond or 0
    if is_cointable then
        diamond = diamond + service.money.coin or 0
    end
    if diamond < enter_diamond then
        return true
    end

    return false
end

function AgentCMD.start_match(...)
	return service:start_match(...)
end

function AgentCMD.end_match(...)
	return service:end_match(...)
end

function AgentCMD.get_diamond()
    if service.money then
        return service.money.diamond
    end
    return 0
end

function AgentCMD.clear_tableinfo(rid)
    return service:leave_table(rid, service.gamenode, service.table_index, service.money.diamond)
end

function AgentCMD.inc_upvoted_num(...)
    return service:inc_upvoted_num(...)
end

function AgentCMD.inc_downvoted_num(...)
    return service:inc_downvoted_num(...)
end

function AgentCMD.get_agentstate()
    if service then
        return service.state
    end
end

function AgentCMD.game_sub_coin(...)
	return service:game_sub_coin(...)
end

function AgentCMD.game_sub_diamond(...)
	return service:game_sub_diamond(...)
end

function AgentCMD.game_balance_coin(...)
	return service:game_balance_coin(...)
end

function AgentCMD.game_half_coin(...)
	return service:game_half_coin(...)
end

function AgentCMD.reduce_prop(...)
	return service:reduce_prop(...)
end

function AgentCMD.jackpot_sub_diamond(...)
    return service:jackpot_sub_diamond(...)
end

function AgentCMD.jackpot_sub_coin(...)
    return service:jackpot_sub_coin(...)
end

function AgentCMD.open_jackpot_diamond(...)
    return service:open_jackpot_diamond(...)
end

function AgentCMD.open_jackpot_coin(...)
    return service:open_jackpot_coin(...)
end

function AgentCMD.check_role_inmatch(...)
	return service:check_role_inmatch(...)
end

function AgentCMD.send_massmail(...)
    return service:send_massmail(...)
end

function AgentCMD.update_withdraw_state(...)
    return service:update_withdraw_state(...)
end

function AgentCMD.refresh_money(...)
	return service:refresh_money(...)
end

return AgentCMD